

rm(list = ls())

library(tidyverse)
library(readxl)
library(writexl)
library(haven)

#######YouGov 2016########

YouGovRaw <- read_csv("YouGovRaw.csv")

YouGovTemp <- YouGovRaw %>% 
                mutate(
                #outcome variables - congressional
                sell_for_25_less = ifelse(Q5 == 1, 1, 0),
                sell_for_50_less = ifelse(Q5 == 1 | Q6 == 1, 1, 0),
                sell_for_100_less = ifelse(Q5 == 1 | Q6 == 1 | Q7 == 1, 1, 0),
                sell_for_200_less = ifelse(Q5 == 1 | Q6 == 1 | Q7 == 1 | Q8 == 1, 1, 0),
                sell_for_500_less = ifelse(Q5 == 1 | Q6 == 1 | Q7 == 1 | Q8 == 1 | Q9 == 1, 1, 0),
                sell_for_1000_less = ifelse(Q5 == 1 | Q6 == 1 | Q7 == 1 | Q8 == 1 | Q9 == 1 | Q10 == 1, 1, 0),
                #outcome variables - presidential
                sell_for_25_less_pres = ifelse(Q12 == 1, 1, 0),
                sell_for_50_less_pres = ifelse(Q12 <= 2, 1, 0),
                sell_for_100_less_pres = ifelse(Q12 <= 3, 1, 0),
                sell_for_200_less_pres = ifelse(Q12 <= 4, 1, 0),
                sell_for_500_less_pres = ifelse(Q12 <= 5, 1, 0),
                sell_for_1000_less_pres = ifelse(Q12 <= 6, 1, 0),
                #covariates
                income = ifelse(faminc == 97, NA, ifelse(faminc == 31, 12, faminc)),
                democrat_strong = ifelse(pid7 == 1, 1, 0),
                republican_strong = ifelse(pid7 == 7, 1, 0),
                age = 2016 - birthyr,
                female = ifelse(gender == 2, 1, 0),
                education = educ - 1,
                religion = 5 - pew_religimp,
                white = ifelse(race == 1, 1, 0),
                black = ifelse(race == 2, 1, 0),
                hispanic = ifelse(race == 3, 1, 0),
                asian = ifelse(race == 4, 1, 0),
                other = ifelse(race == 5 | race == 6 | race == 7 | race == 8, 1, 0),
                registered = ifelse(votereg == 3, NA, ifelse(votereg == 1, 1, 0)),
                district = ifelse(is.na(CD) == T, NA,
                                  paste(state_code, formatC(CD, width = 2, format = "d", flag = "0"), sep = "-")),
                #from Cook 2016 House Race Ratings - Nov 7, 2016
                competitive = ifelse(is.na(district) == T | is.na(state) == T, NA, 
                                ifelse(district == "FL-10" | district == "MD-06" | district == "NY-25" | district == "VA-04" | district == "AZ-01" | district == "CA-07" | district == "CA-24" | district == "FL-13" |
                                       district == "NH-01" | district == "NV-04" | district == "NY-03" | district == "MN-08" | district == "NE-02" | district == "CA-10" | district == "CA-25" | district == "CA-49" |
                                       district == "CO-06" | district == "FL-07" | district == "FL-26" | district == "IA-01" | district == "IL-10" | district == "ME-02" | district == "MN-02" | district == "NJ-05" |
                                       district == "NV-03" | district == "NY-19" | district == "PA-08" | district == "TX-23" | district == "VA-10" | district == "AK-01" | district == "CA-21" | district == "FL-18" |
                                       district == "IA-03" | district == "IN-09" | district == "KS-03" | district == "MI-01" | district == "MI-08" | district == "MN-03" | district == "NY-22" | district == "PA-16" |
                                       district == "UT-04" | district == "AZ-02" | district == "CO-03" | district == "FL-02" | district == "FL-27" | district == "IL-12" | district == "IN-02" | district == "MI-07" |
                                       district == "MT-01" | district == "NY-01" | district == "NY-21" | district == "NY-23" | district == "NY-24" | district == "PA-06" | district == "VA-05" | district == "WI-08",
                                       1, 0)),
                #other
                LE_tr = A1, LE_c = A2, #list experiment for vote buying
                vb = ifelse(Q3 == 3, NA, ifelse(Q3 == 1, 1, 0)), #direct question for vote buying
                vb_text = Q4, #types of gifts received
                defect = ifelse(Q11 == 3, NA, ifelse(Q11 == 2, 1, 0)),
                weight
                )

YouGovClean = YouGovTemp %>% 
              select(
                contains("sell"), income,
                contains("strong"), pid7,
                age, female, education, religion,
                white, black, hispanic, asian, other,
                registered, contains("state"), 
                district, competitive,
                contains("LE"), contains("vb"),
                defect, caseid, weight
              )

YouGovClean_DF <- as.data.frame(YouGovClean)

#write_csv(YouGovClean_DF, "YouGovClean.csv", na = "")



######MTurk 2018######

rm(list = ls())


MTurkRaw <- read_csv("MTurkRaw.csv")

MTurkTemp <- MTurkRaw %>% 
              #keeping only observations with responses to vote selling variables, per pre-analysis plan
              mutate(sell_vars = coalesce(control_25, tr1_25, tr2_25)) %>%
              drop_na(sell_vars) %>%
                  
                  mutate(
                  #outcome variables - congressional
                    sell_for_25_less = ifelse(coalesce(control_25, tr1_25, tr2_25) == 1, 1, 0),
                    sell_for_50_less = ifelse(coalesce(control_50, tr1_50, tr2_50) == 1 |
                                              sell_for_25_less == 1, 1, 0),
                    sell_for_100_less = ifelse(coalesce(control_100, tr1_100, tr2_100) == 1 |
                                                sell_for_50_less == 1, 1, 0),
                    sell_for_200_less = ifelse(coalesce(control_200, tr1_200, tr2_200) == 1 |
                                                 sell_for_100_less == 1, 1, 0),
                    sell_for_500_less = ifelse(coalesce(control_500, tr1_500, tr2_500) == 1 |
                                                 sell_for_200_less == 1, 1, 0),
                    sell_for_1000_less = ifelse(coalesce(control_1000, tr1_1000, tr2_1000) == 1 |
                                                 sell_for_500_less == 1, 1, 0),
                    #outcome variables - presidential
                    sell_for_25_less_pres = ifelse(pres_vb == 1, 1, 0),
                    sell_for_50_less_pres = ifelse(pres_vb <= 2, 1, 0),
                    sell_for_100_less_pres = ifelse(pres_vb <= 3, 1, 0),
                    sell_for_200_less_pres = ifelse(pres_vb <= 4, 1, 0),
                    sell_for_500_less_pres = ifelse(pres_vb <= 5, 1, 0),
                    sell_for_1000_less_pres = ifelse(pres_vb <= 6, 1, 0),
                    #democracy variables
                    dem_impt = ifelse(dem_imp_1 == 1, 1, dem_imp_1 - 19), #this is needed to revert to 1 to 10 scale b/c of coding problem in Qualtrics
                    vote_matters = elections_close,
                    #covariates
                    income = na_if(income, 17),
                    democrat_strong = ifelse(party == 1, 1, 0),
                    republican_strong = ifelse(party == 7, 1, 0),
                    pid7 = na_if(party, 99), # note that 99 = no preference
                    female = ifelse(male == 2, 1, 0),
                    education = ifelse(ed > 6, 6, ed) - 1,
                    white = ifelse(race == 1, 1, 0),
                    black = ifelse(race == 3, 1, 0),
                    hispanic = ifelse(race == 2, 1, 0),
                    asian = ifelse(race == 5, 1, 0),
                    other = ifelse(race == 4 | race == 6 | race == 7 | race == 8, 1, 0),
                    registered = ifelse(party_reg == 3, NA, ifelse(party_reg == 1, 1, 0)),
                    #from Cook 2018 House Race Ratings
                    competitive = ifelse(is.na(district) == T, NA, 
                                         ifelse(district=="CA7" | district=="CA16" | district=="CA49" | district=="FL7" | district=="MN7"  | district=="NH1" | district=="NJ2" | district=="NJ5" | 
                                                district=="PA5" | district=="PA6" | district=="PA8" | district=="PA17" | district=="AZ1" |
                                                district=="AZ2" | district=="CO6" | district=="FL27" | district=="IA1" | district=="IL6" | district=="KS3" | district=="MI11" | district=="MN2" | 
                                                district=="MN3" | district=="NJ11" | district=="NV3" | district=="NV4" | district=="PA7" | district=="VA10" | district=="WA8" | district=="MN1" | 
                                                district=="CA10" | district=="CA25" | district=="CA39" | district=="CA45" | district=="CA48" | district=="FL15" | district=="FL26" | district=="GA6" | 
                                                district=="IA3" | district=="IL14" | district=="KS2" | district=="KY6" | district=="ME2" | district=="MI8" | district=="NC9" | district=="NC13" | 
                                                district=="NJ3" | district=="NJ7" | district=="NM2" | district=="NY19" | district=="NY22" | district=="OH12" | district=="PA1" | district=="PA10" | 
                                                district=="TX7" | district=="TX32" | district=="UT4" | district=="VA2" | district=="VA7" | district=="AK0" | district=="CA50" | district=="FL6" | 
                                                district=="FL16" | district=="FL18" | district=="FL25" | district=="GA7" | district=="IA4" | district=="IL12" | district=="IL13" | district=="MI6" | 
                                                district=="MN8" | district=="MO2" | district=="MT0" | district=="NC2" | district=="NE2" | district=="NY11" | district=="NY24" | district=="NY27" | 
                                                district=="OH1" | district=="PA16" | district=="SC1" | district=="TX22" | district=="TX23" | district=="VA5" | district=="WA3" | district=="WA5" | 
                                                district=="WI1" | district=="WV3" | district=="AR2" | district=="AZ6" | district=="AZ8" | district=="CA1" | district=="CA4" | district=="CA21" | 
                                                district=="CA22" | district=="CO3" | district=="IN2" | district=="MI1" | district=="MI3" | district=="MI7" | district=="NC8" | district=="NY1" | 
                                                district=="NY2" | district=="NY21" | district=="NY23" | district=="OH10" | district=="OH14" | district=="OK5" | district=="PA14" | district=="TX2" | 
                                                district=="TX6" | district=="TX10" | district=="TX21" | district=="TX24" | district=="TX31" | district=="WI6" | district=="WV2", 
                                                1, 0)),
                    #other
                    vote_intent = ifelse(vote2018 == 1 | vote2018 == 2, 1, 0),
                    screener1 = ifelse(Screener1 == 27, 1, 0), # this is choice 2 and 7 (formatted as 27)
                    screener2 = ifelse(Screener2 == 47, 1, 0), #this is choice 4 and 7
                    screener = screener1 + screener2,
                    LE_tr = list_tr,
                    LE_c = list_control,
                    vb = ifelse(vb == 3, NA, ifelse(vb == 1, 1, 0)),
                    defect = ifelse(coalesce(control_credcom, tr1_credcom, tr2_credcom) == 3, NA,
                                    ifelse(coalesce(control_credcom, tr1_credcom, tr2_credcom) == 2, 1, 0)),
                    pres_defect = ifelse(pres_credcom == 3, NA, ifelse(pres_credcom == 2, 1,  0))
               )


MTurkClean = MTurkTemp %>% select(
                                contains("sell"), dem_impt, vote_matters,
                                income, contains("strong"), pid7,
                                age, female, education,
                                white, black, hispanic, asian, other,
                                registered, contains("state"), 
                                district, competitive,
                                contains("LE"), vb, vb_text,
                                contains("defect"), vote_intent,
                                -contains("sell_vars")
                              )

MTurkClean_DF <- as.data.frame(MTurkClean)

#write_csv(MTurkClean_DF, "MTurkClean.csv", na = "")





